#include <stdio.h>
#include <stdlib.h>
#include "crosslist.h"

int init_cross_list(PCrossList L, const ElemType *A, int m,int n){
    int cnt = 0;
    L -> cols = n; L -> rows = m;
    L -> colhead = malloc(sizeof(OLink) * (n+1));
    L -> rowhead = malloc(sizeof(OLink) * (m+1));
    
    OLink *tmp = malloc(sizeof(OLink) * (n + m + 4));
    
    for(int i=1;i<=n;i++) L -> colhead[i] = tmp[i] = malloc(sizeof(OLNode));
    for(int i=1;i<=m;i++) L -> rowhead[i] = tmp[i + n + 1] = malloc(sizeof(OLNode));

    for(int _=0;_<m*n;_++) {
        int i = _ / n + 1, j = _ % n + 1, v = A[_];
        if(!v) continue;
        ++cnt;
        OLNode *node = malloc(sizeof(OLNode));
        node -> col = j; node -> row = i; node -> value = v;
        
        // row
        if(!L -> rowhead[i]) tmp[i + n + 1] = node;
        else L -> rowhead[i] -> right = node;
        L -> rowhead[i] = node;
    
        // col
        if(!L -> colhead[j])  tmp[j] = node;
        else L -> colhead[j] -> down = node;
        L -> colhead[j] = node;
    }

    for(int i=1;i<=n;i++) L -> colhead[i] = tmp[i];
    for(int i=1;i<=m;i++) L -> rowhead[i] = tmp[i + n + 1];
    free(tmp);

    L -> nums = cnt;
    return cnt;
}


int del_cross_list(PCrossList L, ElemType k){
    int n = L -> rows, m = L -> cols, cnt = 0;
    for(int i=0;i<n;i++) {
        while(L -> rowhead[i] && L -> rowhead[i] -> value == k) L -> rowhead[i] = L -> rowhead[i] -> right;
        OLNode *cur = L -> rowhead[i];
        while(cur && cur -> right) {
            while(cur -> right && cur -> right -> value == k) cur -> right = cur -> right -> right;
            cur = cur -> right;
        }
    }
    for(int i=0;i<m;i++) {
        while(L -> colhead[i] && L -> colhead[i] -> value == k) {
            OLNode *tmp = L -> colhead[i];
            L -> colhead[i] = L -> colhead[i] -> down;
            free(tmp); ++cnt;
        }
        OLNode *cur = L -> colhead[i];
        while(cur && cur -> down) {
            while(cur -> down && cur -> down -> value == k) {
                OLNode *tmp = cur -> down;
                cur -> down = cur -> down -> down;
                free(tmp); ++cnt;
            }
            cur = cur -> down;
        }
    }
    return cnt;
}

// ------------
// |   TEST   | 
// ------------ 

int main() {
    int n, m; scanf("%d %d", &n, &m);
    int* data = malloc(sizeof(int) * n * m);
    for(int i=0;i<n*m;i++) scanf("%d", data + i);
    CrossList cl;
    init_cross_list(&cl, data, n, m);
    printf("%d", cl.colhead[2] -> down -> value);
}